<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">

      <el-form-item label="交税人" prop="informName">
        <el-input v-model="queryParams.informName" placeholder="请输入交税人的名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="是否交税" prop="notice">
         <el-select v-model="queryParams.notice" placeholder="请选择是否缴税" clearable>
            <el-option v-for="dict in dict.type.notice" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="类别" prop="personId">
         <el-select v-model="queryParams.personId" placeholder="请选择类别" clearable>
            <el-option v-for="dict in dict.type.person_id" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:inform:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:inform:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:inform:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:inform:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="informList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="informId" v-if="false" />
      <el-table-column label="名称" align="center" prop="informName" />
      <el-table-column label="类别" align="center" prop="personId">
           <template slot-scope="scope">
                <dict-tag :options="dict.type.person_id" :value="scope.row.personId" />
           </template>
      </el-table-column>
      <el-table-column label="QQ邮箱" align="center" prop="email_address" />
      <el-table-column label="m_id" align="center" prop="m_id" v-if="false" />
      <el-table-column label="交税截止时间" align="center" prop="time" width="180" sortable>
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="状态" align="center" prop="notice">
           <template slot-scope="scope">
                <dict-tag :options="dict.type.notice" :value="scope.row.notice" />
           </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:inform:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:inform:remove']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-message" @click="send(scope.row)" v-hasPermi="['system:inform:send']">发送通知</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加易告知管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="交税人" prop="informName">
          <el-input v-model="form.informName" placeholder="请输入交税人的姓名" />
        </el-form-item> -->
        <el-form-item label="类别" prop="personId">
          <el-select v-model="form.personId" placeholder="会员类别" @change="checkPerson()">
            <el-option v-for="dict in dict.type.person_id" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="交税人" prop="chuanmingzi">
          <el-select v-model="chuanmingzi" placeholder="会员名称" @change="getEamil()">
            <el-option v-for="(item, index) in nameForm" :key="index" :label="item.memberName" :value="item.memberName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="邮箱" prop="emailAddress">
          <el-input v-model="email.emailAddress" :readonly="true" placeholder="请输入交税人的QQ邮箱" />

        </el-form-item>

        <el-form-item label="截止时间" prop="time" :verify="timeValidateFunc">
          <el-date-picker clearable v-model="form.time" type="date" value-format="yyyy-MM-dd" :picker-options="pickerOptions" placeholder="请选择交税的截止时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="状态">
          <!-- <el-input v-model="email.payState" placeholder="请输入状态" /> -->
          <el-select v-model="email.payState" placeholder="纳税状态" clearable>
                      <el-option v-for="dict in dict.type.notice" :disabled="true" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
                    </el-select>
          <!-- <el-option v-for="dict in dict.type.notice" :key="dict.value" :label="dict.label" :value="dict.value">
          </el-option> -->
        </el-form-item>
        <!--  <el-form-item label="状态" prop="notice">
          <el-select v-model="form.notice" placeholder="状态">
            <el-option v-for="dict in dict.type.notice" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item> -->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改易告知管理对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="交税人" prop="informName">
          <el-input v-model="form.informName" placeholder="请输入交税人的姓名" readonly />
        </el-form-item>
        <el-form-item label="类别" prop="personId">
          <el-select v-model="form.personId" placeholder="会员类别">
            <el-option v-for="dict in dict.type.person_id" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" disabled></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="邮箱" prop="email_address">
          <el-input v-model="form.email_address" placeholder="请输入qq邮箱" readonly />
        </el-form-item>
        <el-form-item label="截止时间" prop="time" :verify="timeValidateFunc">
          <el-date-picker clearable v-model="form.time" type="date" value-format="yyyy-MM-dd" :picker-options="pickerOptions" placeholder="请选择交税的截止时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="状态" prop="notice">
          <el-select v-model="form.notice" placeholder="状态">
            <el-option v-for="dict in dict.type.notice" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" disabled></el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发送邮件弹窗 -->
    <el-dialog title="发送通知" :visible.sync="emails" width="500px" append-to-body>
      <el-form ref="form" :model="form" :ruless="rules" label-width="80px">
        <el-form-item label="邮箱" prop="to">
          <el-input v-model="to" placeholder="请输入邮箱" :readonly="true" />
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="subject" placeholder="请输入标题" :readonly="true" />
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="text" :min-height="192" :readonly="true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitEmail">发 送</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listInform, getInform, delInform, addInform, updateInform, sendTextMail, addNashuixinxi, getPerson, getEmailByName } from '@/api/system/inform'

export default {
  name: 'Inform',
  dicts: ['notice', 'person_id', 'payState'],
  data() {
    return {
      // 发送邮件 邮箱标题内容
      to: '',
      subject: '缴税通知',
      text: '安徽省电子税务局提醒您,已快到缴税时间,请按时去相关网点缴税纳税。',
      mm_id: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 易告知管理表格数据
      informList: [],
      // 弹出层标题
      title: '',

      // 是否显示弹出层
      open: false,
      open1: false,
      // 是否显示发送Email弹窗
      emails: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        informName: null,
        personId: null,
        email_address: null,
        time: null,
        notice: null
      },
      payNashuixinxi: {
        memberId: null,
        content: null
      },
      // 截至时间校验
      pickerOptions: {
        disabledDate(messageTime) {
          return messageTime.getTime() < Date.now() - 8.64e7
        }
      },
      //
      chuanmingzi: null,
      // 表单参数
      form: {},
      //1.二级联动接收参数
      nameForm: {},
      //2.通过名称获取邮箱
      email: {},

      // 表单校验
      rules: {
        // 邮箱的校验
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            validator: function (rule, value, callback) {
              if (/^([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value) == false) {
                callback(new Error('请输入正确邮箱'))
              } else {
                //校验通过
                callback()
              }
            },
            trigger: 'blur'
          }
        ],
        // 交税人校验
        // informName: [
        //   { required: true, message: '姓名不能为空', trigger: 'blur' },
        //   { min: 2, message: '姓名最少为两个字符', trigger: 'blur' }
        // ],
        // 类别
        personId: [{ required: true, message: '类别不能为空', trigger: 'blur' }],
        // 状态
        notice: [
          { required: true, message: '状态不能为空', trigger: 'blur' }],
        // 时间
        time: [{ required: true, message: '请选择缴税截至时间', trigger: 'blur' }]
      }
    }
  },
  created() {},
  mounted() {
    this.getList()
    // setInterval(this.getList,1000)
  },
  methods: {
    checkPerson() {
      var person_id = parseInt(this.form.personId)
      getPerson(person_id).then((response) => {
        this.nameForm = response
      })
    },
    getEamil() {
      var memberName = this.chuanmingzi

      getEmailByName(memberName).then((response) => {
        this.email = response
        // if (response.payState == 0) {
        //   response.payState = '已缴税'
        // } else {
        //   response.payState = '未缴税'
        // }
      })
    },

    // 截至时间校验
    timeValidateFunc() {
      return [
        {
          validator: (rule, value, callback) => {
            const start = new Date(this.form.messageTime).getTime()
            if (!value) {
              callback(new Error('请选择时间'))
            } else if (start < Date.now()) {
              callback(new Error('发布时间要大于当前时间!'))
            } else {
              callback()
            }
          }
        }
      ]
    },
    close() {
      this.emails = false
    },
    // 展示发送邮件弹窗
    send(row) {
      if (row.notice == 1) {
        this.m_id = row.m_id
        this.to = row.email_address
        this.emails = true
      } else this.$message.error('用户已交税,不需要通知')
    },
    // 发送邮件
    submitEmail() {
      var to = this.to
      var subject = this.subject
      var text = this.text
      var m_id = this.m_id
      sendTextMail(to, subject, text, m_id).then((response) => {
        // this.getList()
      })
      this.sends()
      this.$modal.msgSuccess('发送成功')
      this.emails = false
    },

    // 发送邮件的时候添加数据
    sends() {
      this.payNashuixinxi.content = this.text
      this.payNashuixinxi.memberId = this.m_id

      addNashuixinxi(this.payNashuixinxi).then((response) => {})
    },
    /** 查询易告知管理列表 */
    getList() {
      this.loading = true
      listInform(this.queryParams).then((response) => {
        this.informList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.open1 = false
      this.reset()
    },
    // 表单重置
    reset() {
      ;(this.form = {
        informId: null,
        informName: null,
        personId: null,
        email_address: null,
        time: null,
        notice: null,
        m_id: null
      }),
        (this.chuanmingzi = null),
        (this.email.emailAddress = null),
        (this.email.payState = null),
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.informId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()

      this.open = true
      this.title = '添加易告知管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const informId = row.informId || this.ids
      getInform(informId).then((response) => {
        this.form = response.data
        this.open1 = true
        this.title = '修改易告知管理'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.form.informName = this.chuanmingzi
      this.form.email = this.email.emailAddress
      this.form.m_id = this.email.memberId
      this.form.notice = this.email.payState
      
      addInform(this.form).then((response) => {
        this.$modal.msgSuccess('新增成功')
        this.open = false
        this.getList()
      })

      this.open = false
      this.getList()
    },
    /* 修改的提交按钮 */
    submitForm1() {
      updateInform(this.form).then((response) => {
        this.$modal.msgSuccess('修改成功')
        this.open1 = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const informIds = row.informId || this.ids
      if (row.notice == 1) {
        this.$message.error('还未缴税,不能删除')
      } else {
        this.$modal
          .confirm('是否确认删除易告知管理编号为"' + informIds + '"的数据项？')
          .then(function () {
            return delInform(informIds)
          })
          .then(() => {
            this.getList()
            this.$message('删除成功')
          })
          .catch(() => {})
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/inform/export',
        {
          ...this.queryParams
        },
        `inform_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
